﻿using Acme.BookStore.accommodationS;
using Acme.BookStore.Orders;
using Acme.BookStore.OrderTable;
using HIS;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore.Accommodations
{
    [ApiExplorerSettings(GroupName = "v1")]
    public class AccommodationService : ApplicationService,IaccommodationServices
    {
        public readonly IRepository<accommodations> accommdarepository;
        public AccommodationService(IRepository<accommodations> accommdarepository)
        {
            this.accommdarepository = accommdarepository;
        }
        /// <summary>
        /// 住宿添加
        /// </summary>
        /// <param name="accommodationsCodeDTO"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost("api/AccommsAdd")]
        public async Task<APIResult> AccommsAdd(accommodationsCodeDTo accommodationsCodeDTO)
        {
            try
            {
                accommodations acctommCodeInfo = ObjectMapper.Map<accommodationsCodeDTo, accommodations>(accommodationsCodeDTO);
                await accommdarepository.InsertAsync(acctommCodeInfo);
                if (acctommCodeInfo == null)
                {
                    return new APIResult()
                    {
                        Code = CodeEnum.error,
                        Message = "添加失败"
                    };
                }
                return new APIResult()
                {
                    Code = CodeEnum.success,
                    Message = "住宿添加成功"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 住宿订单的删除
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpDelete("api/AccommsInfoDelete")]
        public async Task<APIResult> AccommsInfoDelete(string Ids)
        {
            try
            {
                var idList = Ids.Split(',').Select(x => Convert.ToInt32(x)).ToList();
                var acctommTypes = await accommdarepository.GetListAsync(x => idList.Contains(x.Id));

                if (acctommTypes == null || !acctommTypes.Any())
                {
                    return new APIResult()
                    {
                        Code = CodeEnum.error,
                        Message = "删除失败，未找到指定的商品"
                    };
                }

                foreach (var roomType in acctommTypes)
                {
                    await accommdarepository.DeleteAsync(roomType);
                }

                return new APIResult()
                {
                    Code = CodeEnum.success,
                    Message = "删除成功"
                };
            }
            catch (Exception ex)
            {
                return new APIResult()
                {
                    Code = CodeEnum.error,
                    Message = $"删除失败: {ex.Message}"
                };
            }
        }
        /// <summary>
        /// 住宿订单展示
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet("api/AccommsInfoShow")]
        
        public async Task<APIResult<List<accommodationsCodeDTo>>> AccommsInfoShow(string Name)
        {
            try
            {
                var list = await accommdarepository.GetListAsync(x => x.IsDeleted == false);
                if (Name != null)
                {
                    list = list.Where(x => x.Name == Name).ToList();
                }
                return new APIResult<List<accommodationsCodeDTo>>()
                {
                    Code = CodeEnum.success,
                    Message = "查询成功",
                    Data = ObjectMapper.Map<List<accommodations>, List<accommodationsCodeDTo>>(list.ToList())
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 住宿订单更新
        /// </summary>
        /// <param name="accommodationUpdataDTo"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut("api/AccommsUpdate")]
        public async Task<APIResult> AccommsUpdate(accommodationUpdataDTO accommodationUpdataDTo)
        {
            try
            {
                var acctommType = await accommdarepository.FirstOrDefaultAsync(x => x.Id == accommodationUpdataDTo.Id);
                if (acctommType == null)
                {
                    return new APIResult()
                    {
                        Code = CodeEnum.error,
                        Message = "商品未找到"
                    };
                }

                ObjectMapper.Map(accommodationUpdataDTo, acctommType);
                await accommdarepository.UpdateAsync(acctommType);

                return new APIResult()
                {
                    Code = CodeEnum.success,
                    Message = "商品修改成功"
                };
            }
            catch (Exception ex)
            {
                return new APIResult()
                {
                    Code = CodeEnum.error,
                    Message = $"修改失败: {ex.Message}"
                };
            }
        }
    }
}
